<template>
  <el-dialog
    :visible="dialogVisible"
    width="500px"
    :title="editForm?'编辑':'新增'"
    @close="$emit('close')"
  >
    <el-form
      ref="form"
      label-width="110px"
      :model="form"
      :rules="rules"
    >

      <el-form-item
        label="Nas盘"
        prop="nasId"
      >
        <el-select
          v-model="form.nasId"
          autofocus
          placeholder=""
          clearable
          style="width: 100%;"
        >
          <el-option
            v-for="item in enabledNasList"
            :key="item.id"
            :label="item.nasName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item
        label="库名"
        prop="dbName"
      >
        <el-input
          v-model="form.dbName"
          placeholder=""
          clearable
        />
      </el-form-item>
    </el-form>

    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        @click="$emit('close')"
      >取 消</el-button>
      <el-button
        type="primary"
        @click="submit"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import coldApi from '@/api/coldStandby/index'
const form = {
  'nasId': '',
  'dbName': '',
  'isEnable': '0'
}
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    editForm: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      form: {...form},
      enabledNasList: []
    }
  },
  computed: {
    rules() {
      return {
        tblDatabaseName: [
          { required: !(this.form.isDefaultRule === '是'), message: '请输入库名', trigger: 'blur' }
        ],
        tblName: [
          { required: true, message: '请输入表名', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.form = {...form}
      } else {
        coldApi.getUsefulNas().then((res) => {
          this.enabledNasList = res
        })
        if (this.editForm) {
          this.form = this.editForm
        }
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.editForm) {
            coldApi.editNasDatabaseConfig(this.form).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('close')
            })
          } else {
            console.log(this.form)
            coldApi.addNasDatabaseConfig(this.form).then(res => {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.$emit('close')
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
